﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace File的简单操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //static FileStream Create(string path)
            //如果文件不存在就创建，如果存在，覆盖之前的文件后重新创建
            //string path = "D:\\test.txt";
            //File.Create(path);

            ////static bool Exists(string path)
            ////判断文件是否存在
            //try
            //{
            //    bool v = File.Exists(path);
            //    if (v)
            //    {
            //        Console.WriteLine("文件存在");
            //    }
            //}
            //catch (FileNotFoundException ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //}
            //finally
            //{

            //}


            //static void Copy(string sourceFileName, string destFileName)
            //复制文件到指定位置
            //这里需要指定文件名
            //File.Copy("D:\\test.txt", "E:\\test1.txt");

            //static void Delete(string path)
            //删除指定路径下的文件
            //File.Delete(path);

            //static void Move(string sourceFileName, string destFileName)
            //移动（剪切）文件。利用剪切可以更改文件名和文件路径
            //File.Move(path, "E:\\实验.txt");


            //string path = "D:\\test.txt";

            ////static IEnumerable<string> ReadLines(string path)
            ////static IEnumerable<string> ReadLines(string path, Encoding encoding)
            ////读取文件中所有的数据，返回值迭代接口类型
            //IEnumerable<string> enumerable = File.ReadLines(path, Encoding.UTF8);
            //IEnumerator<string> enumerator = enumerable.GetEnumerator();
            //while(enumerator.MoveNext())
            //{
            //    Console.WriteLine(enumerator.Current);
            //}
            //Console.WriteLine();

            ////static string[] ReadAllLines(string path)
            ////static string[] ReadAllLines(string path, Encoding encoding)
            ////读取文件中所有数据，返回字符串数组类型
            //string[] strs = File.ReadAllLines(path, Encoding.UTF8);
            //foreach(string str in strs)
            //{
            //    Console.WriteLine(str);
            //}
            //Console.WriteLine();

            ////static string ReadAllText(string path)
            ////static string ReadAllText(string path, Encoding encoding)
            ////读取文件中所有数据，返回字符串类型
            //string v = File.ReadAllText(path, Encoding.UTF8);
            //Console.WriteLine(v + "\n");

            ////static byte[] ReadAllBytes(string path)
            ////读取文件中所有的数据，返回字节数组类型
            //byte[] bytes = File.ReadAllBytes(path);
            //string s = Encoding.UTF8.GetString(bytes);
            //Console.WriteLine(s);

            string path = @"D:\\test1.txt";

            //static void WriteAllLines(string path, string[] contents)
            //static void WriteAllLines(string path, string[] contents, Encoding encoding)
            //向指定文件中写入字符串数组中的数据
            File.WriteAllLines(path, new string[] { "你好，世界", "Hello World" });

            //static void WriteAllText(string path, string contents)
            //static void WriteAllText(string path, string contents, Encoding encoding)
            //向指定文件中写入字符串数据，覆盖原本内容
            File.WriteAllText(path, "张三");

            //static void WriteAllBytes(string path, byte[] bytes)
            //向指定文件中写入字节数组中的数据,也是覆盖
            string str = "长沙";
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            File.WriteAllBytes(path, bytes);

            //static void AppendAllText(string path, string contents)
            //向指定文件中追加字符串数据
            File.AppendAllText(path, "雨花区");

        }
    }
}

